﻿
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions
{
    public static class SysPointExtension
    {
        /// <summary>
        /// 弧长
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static double Lenght(this ISysPoint value)
        {
            if (value is ArcPoint)
            {
                var arcPoint = value as ArcPoint;
                return (arcPoint.ToEntity() as Arc).Length;
            }
            else
            {
                var line = value as LinePoint;
                return (line.ToEntity() as Line).Length;
            }
        }

        /// <summary>
        /// 根据弧上计算角度
        /// </summary>
        /// <param name="value"></param>
        /// <param name="l"></param>
        /// <returns></returns>
        public static double GetDegree(this ISysPoint value, double l)
        {
            if (value is ArcPoint)
            {
                var arcPoint = value as ArcPoint;
                return 180 * l / (Math.PI * arcPoint.R);
            }
            else
            {
                throw XSException.Oh("传入的不是弧");
            }
        }

        /// <summary>
        /// 根据弧长 获取弧上的某一个点  
        /// </summary>
        /// <param name="value"></param>
        /// <param name="v"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static Point3d GetByArcPoint(this ISysPoint value, double length)
        {

            if (value is ArcPoint)
            {
                var arcPoint = value as ArcPoint;
                var degree = (arcPoint.GetDegree(length) + arcPoint.StartAngle) * Math.PI / 180;
                return new Point3d(arcPoint.R * Math.Cos(degree) + arcPoint.OX, 0, arcPoint.R * Math.Sin(degree) + arcPoint.OY);
            }
            else
            {
                var olidLine = value as LinePoint;
                var line = new Line(new Point3d(olidLine.X, olidLine.Y,0), new Point3d(olidLine.X1, olidLine.Y1,0));
                //TODO: 没写完
                line.GetPointAtDist(length);
                return new Point3d();
            }
        }

        /// <summary>
        /// 将一个弧分割成2个弧  包含直线 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="p"></param>
        /// <returns></returns>

        public static List<ISysPoint> ArcSegmentation(this ISysPoint value, Point3d p)
        {
            var list = new List<ISysPoint>();
            if (value is ArcPoint)
            {
                var arcPoint = value as ArcPoint;
                double startAngle = arcPoint.StartAngle;
                double totalAngle = Math.Atan((p.Y - arcPoint.OY) / p.X) - arcPoint.StartAngle;
                double endAngle = totalAngle + startAngle;

                var arc = new ArcPoint(arcPoint.OX, arcPoint.OY, arcPoint.R, startAngle, endAngle, totalAngle);
                list.Add(arc);
            }
            else
            {


            }
            return list;
        }

        /// <summary>
        /// 根据角度求出弧上任意一点
        /// </summary>
        /// <param name="sysPoint"></param>
        /// <param name="degree"></param>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Point3d GetByDegreePoint(this ISysPoint sysPoint, double degree, double v = 0)
        {
            var arc = sysPoint as ArcPoint;
            var a = arc.StartAngle + degree;
            var x = arc.OX + arc.R * Math.Cos(a);
            var y = arc.OY + arc.R * Math.Sin(a);
            return new Point3d(x, y, v);
        }


        /// <summary>
        /// 弧R 缩进
        /// </summary>
        /// <param name="points"></param>
        /// <param name="L"></param>
        /// <returns></returns>
        public static ISysPoint ArcRetract(this ISysPoint point, double L, bool isLeft = true)
        {
            if (point is ArcPoint)
            {
                var m = point as ArcPoint;
                var newM = new ArcPoint(m.OX, m.OY, m.R - L, m.StartAngle, m.EndAngle, m.TotalAngle);
                newM.No = m.No;
                newM.Code = m.Code;
                newM.Name = m.Name;
                newM.BizTpyeCode = m.BizTpyeCode;
                return newM;
            }
            else
            {
                var m = point as LinePoint;
                LinePoint l = null;
                if (isLeft)
                {
                    l = new LinePoint(m.X - L, m.Y, m.X1 - L, m.Y1);
                }
                else
                {
                    l = new LinePoint(m.X + L, m.Y, m.X1 + L, m.Y1);
                }
                l.No = m.No;
                l.Code = m.Code;
                l.Name = m.Name;
                l.BizTpyeCode = m.BizTpyeCode;
                return l;
            }

        }



    }
}
